﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering_Rules;
using Catering.View_Operaction;
using System.Data;
using Microsoft.Reporting.WebForms;
using System.Web.Services;

namespace Catering.Catering_Page.Kufang
{
    public partial class ylcbtj : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(ylcbtj));
            if (!IsPostBack)
            {
                LoadCheckBoxData();
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    CheckBoxList1.Items[i].Selected = true;
                }
            }
        }

        private void LoadCheckBoxData()
        {
            T_RawMaterials rawMaterialsRule = new T_RawMaterials();
            List<Catering_Model.E_RawMaterials> listMaterials = rawMaterialsRule.GetAllList(0);
            Bind_Data_to_Controls.Bind_Material_cbo(listMaterials, this.CheckBoxList1);
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            View_Monitor vm = new View_Monitor();
            int companyid = 0;
            int AreaID = Convert.ToInt32(this.DDLTool1.GetDDL_Area());
            int ClassID = Convert.ToInt32(this.DDLTool1.GetDDL_Class());
            int Year = 1949;
            if (!string.IsNullOrEmpty(this.DDLTool1.GetYear())) Year = Convert.ToInt32(this.DDLTool1.GetYear());
            int[] rawid = GetRaw();
            if (Session["UserID"] != null)
            {
                string userid = Session["UserID"].ToString();
                companyid = new Catering_Rules.T_Personnel().GetCompanyIDByPID(Convert.ToInt32(userid));
            }
            string title = "";
            DateTime tempdate = DateTime.MinValue;
            DateTime.TryParse(Year + "-01-01 00:00:11", out tempdate);
            title = Bind_Data_to_Controls.Bind_ReportTitle(AreaID.ToString(), ClassID.ToString(), tempdate, "原料成本统计");
            this.ylcb_title.Text = title;
            DataTable dt = vm.GetYuanLiaoChengBen(companyid, AreaID, ClassID, rawid, Year);
            ReportDataSource rds = new ReportDataSource("DataSet1", dt);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.Refresh();
            ReportViewer1.Visible = true;
        }

        protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CheckBoxList1.SelectedItem.Text.Equals("全部"))
            {
                if (CheckBoxList1.SelectedItem.Selected)
                {
                    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                    {
                        CheckBoxList1.Items[i].Selected = true;
                    }
                }
            }
            else
            {
                int num = 0;
                for (int i = 1; i < CheckBoxList1.Items.Count; i++)
                {
                    if (CheckBoxList1.Items[i].Selected == true)
                    {
                        num = num + 1;
                    }
                }
                if (num == (CheckBoxList1.Items.Count - 1))
                {
                    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                    {
                        CheckBoxList1.Items[i].Selected = false;
                    }
                }
            }
        }

        /// <summary>
        /// 获取选中大类
        /// </summary>
        /// <returns></returns>
        public int[] GetRaw()
        {
            int[] RawId;//食品大类ID
            int selectNum1 = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    selectNum1 += 1;
                }
            }

            RawId = new int[selectNum1];
            int numTemp = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    RawId[numTemp] = Int32.Parse(CheckBoxList1.Items[i].Value);
                    numTemp += 1;
                }
            }
            return RawId;
        }

        [WebMethod]
        public static string GetAjaxChargePerson(string StrClassID, string StrYear, string StrMonth)
        {
            int classid = 0, year = 0, month = 1;
            Int32.TryParse(StrClassID, out classid);
            Int32.TryParse(StrYear, out year);
            Int32.TryParse(StrMonth, out month);
            if (classid <= 0) return "1";
            if (year <= 0) return "2";
            if (month <= 0) return "3";
            return new Catering_Rules.ChargePersonNumber().GetChargePersonNumberByClassIDAndDate(classid, year, month).ToString();
        }

        [WebMethod]
        public static string ExistChargePerson(string StrClassID, string StrYear, string StrMonth)
        {
            int classid = 0, year = 0, month = 1;
            Int32.TryParse(StrClassID, out classid);
            Int32.TryParse(StrYear, out year);
            Int32.TryParse(StrMonth, out month);
            if (classid <= 0) return "1";
            if (year <= 0) return "2";
            if (month <= 0) return "3";
            bool result = new Catering_Rules.ChargePersonNumber().Exists(classid, year, month);
            if (result) return "OK"; else return "NO";
        }

        [WebMethod]
        public static string SetAjaxChargePerson(string StrGroupID, string StrClassID, string StrYear, string StrMonth, string StrNumber)
        {
            int groupid = 0, classid = 0, year = 0, month = 1, number = 0;
            Int32.TryParse(StrGroupID, out groupid);
            Int32.TryParse(StrClassID, out classid);
            Int32.TryParse(StrYear, out year);
            Int32.TryParse(StrMonth, out month);
            Int32.TryParse(StrNumber, out number);
            if (classid <= 0) return "1";
            if (year <= 0) return "2";
            if (month <= 0) return "3";
            Catering_Model.ChargePersonNumber model = new Catering_Rules.ChargePersonNumber().GetModel(classid, year, month);
            if (model != null)
            {
                bool result = new Catering_Rules.ChargePersonNumber().Update(number, classid, year, month);
                if (result) return "6"; else return "7";
            }
            else
            {
                model = new Catering_Model.ChargePersonNumber();
                model.AreaID = groupid;
                model.ClassID = classid;
                model.Date = DateTime.Parse(year + "-" + month + "-" + 1);
                model.CompanyID = new Catering_Rules.Company().GetModelByID(groupid, false).id;
                model.Number = number;
                bool result = new Catering_Rules.ChargePersonNumber().Add(model);
                if (result) return "4"; else return "5";
            }
        }
    }
}